package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SystemDictionary;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.qo.JsonResult;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.qo.SystemDictionaryItemQueryObject;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.service.ISystemDictionaryService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @RequestMapping("/list")
    @RequiresPermissions(value={"字典明细列表","systemDictionaryItem:list"},logical= Logical.OR)
    public String list(@ModelAttribute("qo") SystemDictionaryItemQueryObject queryObject, Model model){
        PageInfo<SystemDictionaryItem> pageResult = systemDictionaryItemService.selectByQuery(queryObject);
        //List<SystemDictionaryItem> list = systemDictionaryItemService.getAll();
        model.addAttribute("result",pageResult);
        model.addAttribute("systemDictionary",systemDictionaryService.getSystemDictionaryById(queryObject.getParentId()));

        //查到所有字典目录 在左边进行回显
        List<SystemDictionary> systemDictionaryList = systemDictionaryService.getAll();
        model.addAttribute("systemDictionaryList",systemDictionaryList);
        return "systemDictionaryItem/list";
    }



//    @RequestMapping("/input")
//    @RequiredPermission({"字典明细添加","systemDictionaryItem:input"})
//    public String input(Long id, Model model){
//        if(id != null){
//            SystemDictionaryItem systemDictionaryItem = systemDictionaryItemService.getSystemDictionaryItemById(id);
//            model.addAttribute("entity",systemDictionaryItem);
//        }
//        return "systemDictionaryItem/input";
//    }
    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value={"字典明细保存或更新","systemDictionaryItem:saveOrUpdate"},logical= Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionaryItem systemDictionaryItem){
        try {
            systemDictionaryItemService.saveOrUpdate(systemDictionaryItem);
            return new JsonResult();
        }catch(Exception e){
            e.printStackTrace();
            return new JsonResult(false,"保存失败");
        }
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value={"字典明细删除","systemDictionaryItem:delete"},logical= Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        try {
            systemDictionaryItemService.delete(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"删除失败");
        }
    }
}
